package com.blockchain.securewallet.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.model.db.Account;
import com.blockchain.securewallet.view.SwipeMenuLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AccountManagerAdapter extends RecyclerView.Adapter<AccountManagerAdapter.AccountManagerItem> {


    private final LayoutInflater mLayoutInflater;

    private Context context;

    private List<Account> accountList = null;

    private ISwitchAccountListener switchAccountListener;

    public void setListener(ISwitchAccountListener listener) {
        this.switchAccountListener = listener;
    }


    public AccountManagerAdapter(Context context, List<Account> accountList) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.accountList = accountList;
    }

    public void setAddresses(List<Account> accountList) {
        this.accountList = accountList;
    }

    @NonNull
    @Override
    public AccountManagerItem onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AccountManagerItem(mLayoutInflater.inflate(R.layout.item_account_history, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AccountManagerItem holder, final int position) {

        int idLength = accountList.get(0).getAccountId().length();
        holder.tv_history_account.setText(accountList.get(position).getAccountId().substring(idLength - 4, idLength));
        holder.tv_history_account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (switchAccountListener != null) {
                    switchAccountListener.onClick(accountList.get(position));
                }
            }
        });
        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (switchAccountListener != null) {
                    switchAccountListener.onDelete(position, accountList.get(position));
                }
            }
        });

        SwipeMenuLayout swl = holder.getSwipeMenuLayout();
        //IOS阻塞式交互、设置左滑动
        swl.setIos(true).setLeftSwipe(true);

    }

    @Override
    public int getItemCount() {
        return accountList.size();
    }

    class AccountManagerItem extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_history_account)
        TextView tv_history_account;

        @BindView(R.id.btn_delete)
        TextView btn_delete;

        SwipeMenuLayout swipeMenuLayout;

        public AccountManagerItem(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.swipeMenuLayout = (SwipeMenuLayout) itemView;
        }

        public SwipeMenuLayout getSwipeMenuLayout() {
            return swipeMenuLayout;
        }

        public void setSwipeMenuLayout(SwipeMenuLayout swipeMenuLayout) {
            this.swipeMenuLayout = swipeMenuLayout;
        }
    }


    public interface ISwitchAccountListener {
        void onClick(Account account);

        void onDelete(int position, Account account);
    }


}
